[SPARK-59018][SQL] Add variant_array_length expression - #58301
Open
WangGuangxin wants to merge 2 commits into
Open
[SPARK-59018][SQL] Add variant_array_length expression#58301WangGuangxin wants to merge 2 commits into
WangGuangxin wants to merge 2 commits into
Conversation
WangGuangxin
force-pushed
the
variant-array-length
branch
from
August 26, 2026 02:15
792a539 to
085a181
Compare
Contributor
Author
|
@bojana-db @uros-b Can you please help review this? |
uros-b
reviewed
Aug 26, 2026
uros-b
left a comment
Member
There was a problem hiding this comment.
@bojana-db @harshmotw-db Could you please help review this PR? Thank you @WangGuangxin!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
This PR adds a new
variant_array_lengthexpression that returns the number of elements in a variant array.If the input is a SQL NULL, the function returns NULL. If the input variant is not an array, including variant null, object, or scalar values, the function returns NULL. If the input is an empty array, the
function returns 0.
Usage examples:
Why are the changes needed?
Spark already supports expanding variant arrays through variant_explode, but using a generator is unnecessarily cumbersome when users only need the array length. Users currently need to explode the value and
count rows, or cast the extracted value into a typed array when the element type is known.
variant_array_length provides a direct scalar operation for a common semi-structured data use case: inspecting array cardinality before filtering, validating, or transforming variant data. It is also cheaper and
easier to optimize than expanding the array when only the number of elements is needed, because the variant binary format stores array size in the array header.
This function also improves parity with Spark's JSON function surface, which already provides json_array_length for JSON strings. variant_array_length provides the corresponding operation for parsed variant
values.
Does this PR introduce any user-facing change?
Yes. This PR adds a new SQL function, variant_array_length.
Example:
returns:
How was this patch tested?
Unit tests were added for variant_array_length, including:
Was this patch authored or co-authored using generative AI tooling?
Generated-by: TraeCode (GPT-5)